
import UtilFuncs from '../../common/tools/utilfuncs.js'
var utilfuncs = UtilFuncs.UtilFuncs;

import LXCOM from '../../common/common.js'

class TSecion {
    constructor() {
        this.st = { x: 0, y: 0 };
        this.end = { x: 0, y: 0 };
    }
}
var AbsorCubePos = {}

AbsorCubePos.calculate = function (pDstGraph, mousePt, offsetPt) {
    var x = mousePt.x;
    var y = mousePt.y;
    var absobPt = { x, y };
    let mindis = -1;
    let lineSecs = this.getSections(pDstGraph, offsetPt);
    for (let i = 0; i < lineSecs.length; i++) {
        let secInfo = lineSecs[i];
        let prePt = secInfo.st;
        let nextPt = secInfo.end;
        var tmpPoint = { x, y };
        var dis = this.pt2LineSecDis(prePt.x, prePt.y, nextPt.x, nextPt.y, tmpPoint, mousePt);
        if (dis >= 0) {
            if (dis < mindis || mindis < 0) {
                mindis = dis;
                absobPt.x = tmpPoint.x;
                absobPt.y = tmpPoint.y;
            }
        }
    }
    return absobPt;
}

AbsorCubePos.getSections = function (pDstGraph, offsetPt) {
    let secArr = [];
    let halfwid = pDstGraph.width / 2;
    let halfhgt = pDstGraph.height / 2;
    let depth = pDstGraph.depth;

    let mtx = pDstGraph.getLocalMatrix();
    let x = 0;
    let y = 0;

    let secUnit = null;

    //前平面
    {
        //前平面left
        secUnit = new TSecion();
        secArr.push(secUnit);
        x = -halfwid;
        y = halfhgt - depth;
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = -halfwid;
        y = -halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //bottom
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid - depth;
        y = -halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //right
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid - depth;
        y = halfhgt - depth;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //top
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = -halfwid;
        y = halfhgt - depth;
        secUnit.end = mtx.MultT({ x, y, z: 0 });
    }

    //顶面
    {
        //left
        secUnit = new TSecion();
        secArr.push(secUnit);
        x = -halfwid + depth;
        y = halfhgt;
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = -halfwid;
        y = halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //bottom
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid - depth;
        y = halfhgt - depth;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //right
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid;
        y = halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //top
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = -halfwid + depth;
        y = halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });
    }

    //右平面
    {
        //left
        secUnit = new TSecion();
        secArr.push(secUnit);
        x = halfwid - depth;
        y = halfhgt - depth;
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid - depth;
        y = -halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //bottom
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid;
        y = -halfhgt + depth;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //right
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid;
        y = halfhgt;
        secUnit.end = mtx.MultT({ x, y, z: 0 });

        //top
        secUnit = new TSecion();
        secArr.push(secUnit);
        secUnit.st = mtx.MultT({ x, y, z: 0 });

        x = halfwid - depth;
        y = halfhgt - depth;
        secUnit.end = mtx.MultT({ x, y, z: 0 });
    }

    secArr.forEach(secInfo => {
        secInfo.st.x = secInfo.st.x + offsetPt.x;
        secInfo.st.y = secInfo.st.y + offsetPt.y;

        secInfo.end.x = secInfo.end.x + offsetPt.x;
        secInfo.end.y = secInfo.end.y + offsetPt.y;
    })

    return secArr;
}

AbsorCubePos.pt2LineSecDis = function (x1, y1, x2, y2, absobPt, mousePt) {
    let x = mousePt.x;
    let y = mousePt.y;
    var tmpPoint = { x, y };
    x = x1;
    y = y1;
    var pt1 = { x, y };

    x = x2;
    y = y2;
    var pt2 = { x, y };

    var linesec = new LXCOM.LGXLineSection();
    linesec.setPt(pt1, pt2);
    var dis = utilfuncs.pt2LineSec(mousePt, linesec, tmpPoint);
    absobPt.x = tmpPoint.x;
    absobPt.y = tmpPoint.y;
    return dis;
}

export default { AbsorCubePos }